﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Finance.EntityFrameworkCore;
using mozhi.smarterp.Finance.Finances.Repository;
using mozhi.smarterp.Finance.Querys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.Finance.Finances
{
    public class JournalTemplateRepository : EfCoreRepository<FinanceDbContext, JournalTemplate , Guid>, IJournalTemplateRepository
    {
        public JournalTemplateRepository(IDbContextProvider<FinanceDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task DeleteAsync(Guid id, bool autoSave = false, CancellationToken cancellationToken = default)
        {
            var item = await DbContext.Set<JournalTemplate>().FirstAsync(p => p.Id == id);

            if (item == null)
                return;

            DbContext.Set<JournalTemplate>().Remove(item);

            await DbContext.SaveChangesAsync();
        }

        public async Task<List<JournalTemplate>> FindAsync(PageQuery query)
        {
            //var dbQuery = query.IncludeDetail ? DbContext.Set<JournalTemplate>().Include(x => x.Items).AsNoTracking() : DbContext.Set<JournalTemplate>().AsNoTracking();

            //var items = await dbQuery.
            //    WhereIf(query.Id.HasValue, x => x.Id == query.Id).
            //    WhereIf(!query.Keyword.IsNullOrEmpty(), x => (x.Name != null && x.Name.Contains(query.Keyword)) || (x.Code != null && x.Code.Contains(query.Keyword)))
            //    .Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize)
            //    .ToListAsync();

            //return items;
            throw new NotImplementedException();
        }

        public async Task<JournalTemplate> FindAsync(Guid id, bool includeDetails = true, CancellationToken cancellationToken = default)
        {
            var dbQuery = includeDetails ? DbContext.Set<JournalTemplate>().Include(x => x.Items).AsNoTracking() : DbContext.Set<JournalTemplate>().AsNoTracking();

            return await dbQuery.FirstOrDefaultAsync(x => x.Id == id);
        }

        public async Task<JournalTemplate> GetAsync(Guid id, bool includeDetails = true, CancellationToken cancellationToken = default)
        {
            var dbQuery = DbContext.Set<JournalTemplate>().Include(x => x.Items);

            return await dbQuery.FirstOrDefaultAsync(x => x.Id == id);
        }

        public async Task<int> GetCountAsync(PageQuery query)
        {
            //var count = await DbContext.Set<JournalTemplate>().AsNoTracking().
            //    WhereIf(query.Id.HasValue, x => x.Id == query.Id).
            //    WhereIf(!query.Keyword.IsNullOrEmpty(), x => (x.Name != null && x.Name.Contains(query.Keyword)) || (x.Code != null && x.Code.Contains(query.Keyword)))
            //    .CountAsync();

            //return count;
            throw new NotImplementedException();
        }
    }
}
